package com.xuecheng.test.rabbitmq.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;


@Configuration
public class RabbitmqConfig {
    @Value("${rabbitmq.host}")
    private String host;
    @Value("${rabbitmq.port}")
    private int port;
    private static final String EXCHANGE_TOPIC_INFORM="exchange_topic_inform";
    private static final String EXCHANGE_EMAIL_ROUTINGKEY ="inform.#.email.#";
    private static final String EXCHANGE_SMS_ROUTINGKEY ="inform.#.sms.#";
    private static final String QUEUE_SMS="queue_sms";
    private static final String QUEUE_EMAIL="queue_email";




    //建立一个连接容器，类型数据库的连接池
    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory =
                new CachingConnectionFactory(host, port);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);// 确认机制
//        connectionFactory.setPublisherReturns(true);
        //发布确认，template要求CachingConnectionFactory的publisherConfirms属性设置为true
        return connectionFactory;
    }
    // RabbitMQ的使用入口
    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    //必须是prototype类型
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(this.connectionFactory());
        template.setMessageConverter(this.jsonMessageConverter());
        template.setMandatory(true);
        return template;
    }

    @Bean(EXCHANGE_TOPIC_INFORM)
    public Exchange exchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_INFORM).durable(true).build();
    }

    @Bean
    public Binding bindingQueueEmail(@Qualifier(QUEUE_EMAIL)Queue queue,
                                     @Qualifier(EXCHANGE_TOPIC_INFORM)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(EXCHANGE_EMAIL_ROUTINGKEY).noargs();
    }
    @Bean
    public Binding bindingQueueSMS(@Qualifier(QUEUE_SMS)Queue queue,
                                     @Qualifier(EXCHANGE_TOPIC_INFORM)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(EXCHANGE_SMS_ROUTINGKEY).noargs();
    }
    /**
     * 队列
     *
     * @return
     */
    @Bean(QUEUE_SMS)
    public Queue queueEmail() {
        return new Queue(QUEUE_SMS, true); //队列持久

    }
    @Bean(QUEUE_EMAIL)
    public Queue queueSms() {
        return new Queue(QUEUE_EMAIL, true); //队列持久

    }


    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
